package com.zbs.framework.core.rolefunction.action;

import com.zbs.framework.common.utils.ResultData;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.dao.rolefunction.params.RoleFunctionParam;
import com.zbs.framework.dao.rolefunction.service.RoleFunctionService;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiParam;
import com.zbs.framework.swagger.annotations.ApiTags;
import com.zbs.framework.swagger.enums.RequestType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Create by Zbs on 2019/04/30.
 */
@ApiTags(value = "角色功能管理", group = "UIM")
@RestController
@RequestMapping("/roleFunction")
public class RoleFunctionController {

    @Autowired
    private RoleFunctionService roleFunctionService;

    /**
     * 编辑角色功能
     *          角色ID
     *          功能ID
     * @return
     */
    @ApiOperation(value = "编辑角色功能",requestType = RequestType.JSON)
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys::editRoleFunction')")
    public ResultData<?> edit(@RequestBody RoleFunctionParam param) {
        roleFunctionService.editRoleFunctions(param.getRoleId(), param.getFunctionIds());
        return ResultUtil.ok();
    }

    /**
     * 获取角色拥有的功能ID数组
     *
     * @param roleId 角色ID
     * @return
     */
    @ApiOperation(value = "获取角色拥有的功能ID数组",response = Long.class)
    @PostMapping("/getFuncIds")
    @PreAuthorize("hasAuthority('sys::getRoleFuncIds')")
    public ResultData<List<Long>> getFuncIds(@ApiParam(value = "角色id",required = true) Integer roleId) {
        return ResultUtil.ok(roleFunctionService.getFuncIds(roleId));
    }
}
